package boy.net
{
   import flash.display.Bitmap;
   import flash.display.DisplayObject;
   import flash.display.Loader;
   import flash.events.Event;
   import flash.events.EventDispatcher;
   import flash.net.FileFilter;
   import flash.net.FileReference;
   
   /**
    * A extend class using FileReference. The main purpose is to upload photos from local easily.
    * @author boy, cjboy1984@gmail.com
    * @usage
    * var selector:PhotoSelector = new PhotoSelector();
    * // bowse
    * selector.browse(onFileSelected, onFileLoaded);
    * // or dispose
    * selector.dispose();
    * 
    * private function onFileLoaded(e:Event):void
    * {
    * }
    * 
    * private function onFileSelected(e:Event):void
    * {
    * }
    */   
   public class PhotoSelector extends EventDispatcher
   {
      // callback handler
      protected var onFileLoaded:Function;
      protected var onFileSelected:Function;
      
      // file
      protected var fileRef:FileReference;
      // loader
      protected var loaded:Boolean;
      protected var fileLdr:Loader;
      
      public function PhotoSelector()
      {
         fileRef = new FileReference();
         fileRef.addEventListener(Event.SELECT, onFrefSelect);
         fileRef.addEventListener(Event.COMPLETE, onFrefLoaded);
         
         fileLdr = new Loader();
         fileLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, onFloaderLoaded);
         
         loaded = false;
      }
      
      // ________________________________________________
      //                                             main
      
      public function browse(FileSelect:Function = null, FileLoaded:Function = null):void
      {
         onFileSelected = FileSelect;
         onFileLoaded = FileLoaded;
         
         fileRef.browse(new Array(new FileFilter("Images(*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png")));
      }

      /**
       * The "reference" of the bitmap content.
       */
      public function get content():Bitmap { return Bitmap(fileLdr.content); }
      
      public function dispose():void
      {
         fileRef.removeEventListener(Event.SELECT, onFrefSelect);
         fileRef.removeEventListener(Event.COMPLETE, onFrefLoaded);
         fileRef = null;
         
         if (loaded) fileLdr.unloadAndStop(true);
         fileLdr.contentLoaderInfo.removeEventListener(Event.COMPLETE, onFloaderLoaded);
         fileLdr = null;
      }
      
      // ################### protected ##################
      
      protected function onFrefSelect(e:Event):void
      {
         fileRef.load();
         
         if (onFileSelected as Function)
         {
            onFileSelected(e);
         }
      }
      
      protected function onFrefLoaded(e:Event):void
      {
         loaded = true;
         fileLdr.loadBytes(fileRef.data);
      }
      
      protected function onFloaderLoaded(e:Event):void
      {
         if (onFileLoaded as Function)
         {
            onFileLoaded(e);
         }
      }
      
      // #################### private ###################
      
      // --------------------- LINE ---------------------
      
   }
   
}